﻿#include "RankingList.h"
#include "Player.h"
#include "ManagerHelper.h"
#include <vector>

#include "ConstantDefine.h"
#include "../share/StringFactory.h"

//#include <string>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>

//-------------------------------------------------------------------------------
RankingList::RankingList()
	: GameObject()
	, m_vecRankFixed(50)
{
	Clear();
}

//-------------------------------------------------------------------------------
RankingList::~RankingList()
{

}

//-------------------------------------------------------------------------------
void RankingList::Clear()
{
	_safe_guard_begin;

	init();
	m_rankType			= 0;
	m_updateTime		= 0;
	m_showCount			= 0;
	m_rankingBufferIdx	= 0;
	m_valueLimit		= 0;
	m_scriptId			= 0;
	m_isUpdated			= 1;

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void RankingList::init()
{
	_safe_guard_begin;

	m_vecRankFixed.clear();

	m_vecTiming.clear();

	_mem_zero(m_rankName, NameStrLen);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void removeAll(std::string &str, char c)
{
	std::string::iterator new_end = remove_if(str.begin(), str.end(), bind2nd(std::equal_to<char>(), c));
	str.erase(new_end, str.end());
}


bool RankSortByM1(const stRankMemberDate &v1, const stRankMemberDate &v2);
void RankingList::UpdateRankListDataAtRegular(std::string& rData
	, uint32_t start, uint32_t count)
{
	_safe_guard_begin;

	Json::Value _rankData;
	JsonReader(rData, _rankData);

	Json::Value::Members _jvTemp = _rankData.getMemberNames();

	uint32_t _size = (uint32_t)(_rankData.size());
	
	std::vector<stRankMemberDate> _tmp;
	_tmp.clear();

	for (uint32_t i = 0; i < _size; ++i)
	{
		stRankMemberDate _tmp_ifs;
		_tmp_ifs.clear();

		uint32_t _eSize = _rankData[_jvTemp[i]].size();
		std::vector<std::string> _vecItem;
		_vecItem.clear();
		for (uint32_t j = 0; j < _eSize; ++j)
		{
			//if (j == (_eSize-1))
			//{
			//	std::string _eStr = _rankData[_jvTemp[i]][j].asString();
			//	for (std::string::iterator it = _eStr.begin(); it != _eStr.end(); ++it)
			//	{
			//		if (*it == "\\")
			//		{

			//		}
			//	}


			//	_vecItem.push_back(_eStr);
			//}
			//else
			{
				std::string _eStr = _rankData[_jvTemp[i]][j].asString();

				_vecItem.push_back(_eStr);
			}
		}

		_tmp_ifs.mRankingCurrent = (uint32_t)atoi(_vecItem[0].c_str());	//排名
		_tmp_ifs.mPlayerId = (uint32_t)atoi(_vecItem[1].c_str());		//角色id
		strcpy(_tmp_ifs.mPlayerName, _vecItem[2].c_str());				//角色名字
		_tmp_ifs.mPlayerPhoto = (uint32_t)atoi(_vecItem[3].c_str());	//角色头像
		_tmp_ifs.mPlayerPhotoBox = (uint32_t)atoi(_vecItem[4].c_str()); //角色头像框
		_tmp_ifs.mPlayerLevel = (uint32_t)atoi(_vecItem[5].c_str());	//玩家等级
		_tmp_ifs.mFc = (uint32_t)atoi(_vecItem[6].c_str());				//战力值
		_tmp_ifs.mValueCurrent = (uint32_t)atoi(_vecItem[7].c_str());	//入榜数值
		_tmp_ifs.mRankType = (uint32_t)atoi(_vecItem[8].c_str());		//榜单类型
		_tmp_ifs.mRankNote = _vecItem[9].c_str();						//排行信息 如：防守阵容

		_tmp.push_back(_tmp_ifs);

	}

	if (!_tmp.empty() && _tmp.size() > 1)
	{
		std::stable_sort(_tmp.begin(), _tmp.end(), RankSortByM1);
		
	}

	uint32_t size_v = count + m_vecRankFixed.size();
	//if (size > 50)
	//{
	//	size = 50;
	//}

	uint32_t size = count + m_vecTiming.size();
	if (size > 50)
	{
		size = 50;
	}

	//m_vecRankFixed.resize(size);
	m_vecTiming.resize(size);
	for (uint32_t i = 0; i < count; ++i)
	{
		//m_vecRankFixed[start].clear();
		//m_vecRankFixed[start] = _tmp[i];

		m_vecTiming[start].clear();
		m_vecTiming[start] = _tmp[i];

		++start;
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
bool RankSortByM1(const stRankMemberDate &v1, const stRankMemberDate &v2)
{
	_safe_guard_begin;

	return v2.mRankingCurrent > v1.mRankingCurrent;//降序排列  --当前数值

	_safe_guard_end;

	return false;
}

//-------------------------------------------------------------------------------
void RankingList::GetOrderedRankListByErea(uint32_t typeId
	, int32_t minRanking, int32_t maxRanking, Json::Value& jv)
{
	_safe_guard_begin;

	if (1 == m_isUpdated)
	{
		SetTimingListIsUpdated(false);

		//更换榜单
		if (m_vecRankFixed.size() < m_vecTiming.size())
		{
			uint32_t _size = m_vecTiming.size();
			m_vecRankFixed.resize(_size);
		}
		std::copy(m_vecTiming.begin(), m_vecTiming.end(), m_vecRankFixed.begin());
	}

	if (m_vecRankFixed.empty())
	{
		return;
	}

	uint32_t endRanking = maxRanking;
	int32_t lastCount = (uint32_t)(m_vecRankFixed.size()) - minRanking;

	if (lastCount <= 0)
	{
		return;
	}

	if (lastCount < maxRanking)
	{
		endRanking = maxRanking;
	}

	Json::Value jvList;
	jvList.clear();
	Json::Value jvTem;
	jvTem.clear();
	for (uint32_t i = minRanking; i < endRanking; ++i)
	{
		if (0 == m_vecRankFixed[i].mPlayerId
			|| i == m_vecRankFixed.size())
		{
			break;
		}

		jvTem.clear();
		jvTem.append(i + 1);								//排名(从第1名起)
		jvTem.append(m_vecRankFixed[i].mPlayerId);			//玩家id
		jvTem.append(m_vecRankFixed[i].mPlayerName);		//玩家名字
		jvTem.append(m_vecRankFixed[i].mPlayerPhoto);		//玩家头像
		jvTem.append(m_vecRankFixed[i].mPlayerPhotoBox);	//玩家头像框
		jvTem.append(m_vecRankFixed[i].mFc);//战力值		//战力值
		jvTem.append(m_vecRankFixed[i].mPlayerLevel);		//玩家等级

		if (!m_vecRankFixed[i].mRankNote.empty())			//英雄信息串
		{
			jvTem.append(m_vecRankFixed[i].mRankNote);
		}
		else
		{
			jvTem.append("");//four heros info is null.
		}

		jvList.append(jvTem);
	}

	if (!jvList.empty())
	{
		jv = jvList;
	}

	_safe_guard_end;

}

uint32_t RankingList::GetRankingByPlayerId(uint32_t playerId)
{
	_safe_guard_begin;

	if (m_vecRankFixed.empty())
	{
		return 0;
	}

	uint32_t _cur_ranking = 0;
	for (std::vector<stRankMemberDate>::iterator itor = m_vecRankFixed.begin(); itor != m_vecRankFixed.end(); ++itor)
	{
		stRankMemberDate _dt = (*itor);
		if (playerId == _dt.mPlayerId)
		{
			_cur_ranking = _dt.mRankingCurrent;
			break;
		}	
	}

	return _cur_ranking;

	_safe_guard_end;

	return 0;
}

//void RankingList::UpdateRankFiexdList(void)
//{
//	//_safe_guard_begin;
//
//	//Json::Value _rankData;
//	//JsonReader(rData, _rankData);
//
//	//Json::Value::Members _jvTemp = _rankData.getMemberNames();
//
//	//uint32_t _size = (uint32_t)(_rankData.size());
//
//	//std::vector<stRankMemberDate> _tmp;
//	//_tmp.clear();
//
//	//for (uint32_t i = 0; i < _size; ++i)
//	//{
//	//	stRankMemberDate _tmp_ifs;
//	//	_tmp_ifs.clear();
//
//	//	uint32_t _eSize = _rankData[_jvTemp[i]].size();
//	//	std::vector<std::string> _vecItem;
//	//	_vecItem.clear();
//	//	for (uint32_t j = 0; j < _eSize; j++)
//	//	{
//	//		std::string _eStr = _rankData[_jvTemp[i]][j].asString();
//
//	//		_vecItem.push_back(_eStr);
//	//	}
//
//	//	_tmp_ifs.mRankingCurrent = (uint32_t)atoi(_vecItem[0].c_str());	//排名
//	//	_tmp_ifs.mPlayerId = (uint32_t)atoi(_vecItem[1].c_str());		//角色id
//	//	strcpy(_tmp_ifs.mPlayerName, _vecItem[2].c_str());				//角色名字
//	//	_tmp_ifs.mPlayerPhoto = (uint32_t)atoi(_vecItem[3].c_str());	//角色头像
//	//	_tmp_ifs.mPlayerPhotoBox = (uint32_t)atoi(_vecItem[4].c_str()); //角色头像框
//	//	_tmp_ifs.mPlayerLevel = (uint32_t)atoi(_vecItem[5].c_str());	//玩家等级
//	//	_tmp_ifs.mFc = (uint32_t)atoi(_vecItem[6].c_str());				//战力值
//
//	//	_tmp.push_back(_tmp_ifs);
//
//	//}
//
//	//if (!_tmp.empty() && _tmp.size() > 1)
//	//{
//	//	std::stable_sort(_tmp.begin(), _tmp.end(), RankSortByM1);
//
//	//}
//
//	//uint32_t size = count + m_vecRankFixed.size();
//	//if (size > 50)
//	//{
//	//	size = 50;
//	//}
//
//	//m_vecRankFixed.resize(size);
//	//for (uint32_t i = 0; i < count; i++)
//	//{
//	//	m_vecRankFixed[start].clear();
//	//	m_vecRankFixed[start] = _tmp[i];
//
//	//	++start;
//	//}
//}

void RankingList::AddMemberToTimingList(std::string memberInfo)
{

	//if (m_vecTiming.empty())
	//{
	//	return;
	//}

	//Json::Value _memberData;
	//JsonReader(memberInfo, _memberData);

	//uint32_t _size = (uint32_t)(_memberData.size());
	//if (_size <= 0 || _size > 8)//_memberData[8]
	//{
	//	return;
	//}
	//
	//stRankMemberDate _stData;
	//_stData.clear();

	//_stData.mPlayerId			= (uint32_t)atoi(_memberData[1].asString().c_str());	//角色ID
	//strcpy(_stData.mPlayerName, _memberData[2].asString().c_str());						//角色名字
	//_stData.mPlayerPhoto		= (uint32_t)atoi(_memberData[3].asString().c_str());	//角色头像
	//_stData.mPlayerPhotoBox		= (uint32_t)atoi(_memberData[4].asString().c_str());	//角色头像框
	//_stData.mPlayerLevel		= (uint32_t)atoi(_memberData[5].asString().c_str());	//玩家等级
	//_stData.mFc					= (uint32_t)atoi(_memberData[6].asString().c_str());	//战力值
	//_stData.mValueCurrent		= (uint32_t)atoi(_memberData[7].asString().c_str());	//入榜数值
	//_stData.mRankType			= (uint32_t)atoi(_memberData[8].asString().c_str());	//榜单类型

	//uint32_t _count = m_vecTiming.size();
	//if (_stData.mValueCurrent >= m_vecTiming[_count - 1].mValueCurrent)
	//{
	//	m_vecTiming[_count - 1] = _stData;

	//	//保持有序
	//	std::stable_sort(m_vecTiming.begin(), m_vecTiming.end(), RankSortByM1);

	//}

}

void RankingList::SetTimingListIsUpdated(uint32_t state)
{
	_safe_guard_begin;

	m_isUpdated = state;

	_safe_guard_end;
}

//---------------------------------------------------------------------------
bool HeroSortByM3(const PlayerHero &v1, const PlayerHero &v2);
bool HeroSortByM3(const PlayerHero &v1, const PlayerHero &v2)
{
	_safe_guard_begin;

	if (v1.mFightingCapacity == v2.mFightingCapacity)
	{
		if (v1.mLevel == v2.mLevel)
		{
			return v1.mHeroId > v2.mHeroId;
		}
		else
		{
			return v1.mLevel > v2.mLevel;
		}
	}
	else
	{
		return v1.mFightingCapacity > v2.mFightingCapacity;	//降序排列  --战斗力
	}

	_safe_guard_end;

	return false;
}
